﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace ARchGL.Platform.WindowsService.Application
{
    /// <summary>
    /// LED屏幕服务类
    /// </summary>
    public class LEDService : LEDBase
    {
        //每发送一条数据都需要
        //1.建立连接 
        //2.发送一次数据 
        //3.断开连接
        //不能建立连接，发送多次数据，断开连接（或不断开连接） 这样会在几天后出现显示异常

        public LEDService()
        {
            UpdateConfig();
        }

        /// <summary>发送矩形区域</summary>
        public bool SendArea(int width, int height)
        {
            var flag = false;
            using (var bmp = new Bitmap(width, height))
            {
                var redBrush = new SolidBrush(Color.Red);//红色笔
                using (var g = Graphics.FromImage(bmp))
                {
                    try
                    {
                        User_RealtimeConnect(cardNumber);

                        //1.设置整个画布，在画布上分左右显示内容
                        g.FillRectangle(redBrush, 0, 0, width, height);//设置背景

                        //4.将画布发送至 LED 显示
                        var hbitmap = bmp.GetHbitmap();
                        if (!User_RealtimeSendData(cardNumber, 0, 0, width, height, hbitmap))
                        {
                            //lblMessage.Text = "实时发送图片1失败";
                        }
                        else
                        {
                            flag = true;
                            //lblMessage.Text = "实时发送图片1成功";
                        }
                        DeleteObject(hbitmap);
                    }
                    catch (Exception ex)
                    {

                    }
                    finally
                    {
                        DisposeConnect();//断开连接
                    }
                }
            }
            return flag;
        }

        public Bitmap SendBitmap(int x, int y, int width, int height, string content)
        {
            var flag = false;
            var fontBold = new Font("宋体", 14, FontStyle.Bold);
            var redBrush = new SolidBrush(Color.Red); //红色笔
            using (var bmp = new Bitmap(width, height))
            {
                var whiteBrush = new SolidBrush(Color.Black);//黑色背景
                using (var g = Graphics.FromImage(bmp))
                {
                    try
                    {
                        User_RealtimeConnect(cardNumber);

                        //1.设置整个画布，在画布上分左右显示内容
                        g.FillRectangle(whiteBrush, 0, 0, width, height);//设置背景
                        //g.RotateTransform(90);

                        //var titleArea = new RectangleF(x, y, width, height); //标题区域
                        var titleArea = new RectangleF(x, y, 96, 48); //标题区域
                        g.DrawString(content, fontBold, redBrush, titleArea); //显示标题文字

                        //4.将画布发送至 LED 显示
                        var hbitmap = bmp.GetHbitmap();
                        if (!User_RealtimeSendData(cardNumber, 0, 0, width, height, hbitmap))
                        {
                            //lblMessage.Text = "实时发送图片1失败";
                        }
                        else
                        {
                            flag = true;
                            //lblMessage.Text = "实时发送图片1成功";
                        }
                        DeleteObject(hbitmap);
                    }
                    catch (Exception ex)
                    {

                    }
                    finally
                    {
                        DisposeConnect();//断开连接
                    }
                    return bmp;
                }
            }
            //return flag;
        }

        /// <summary>发送文字内容至 LED</summary>
        /// <param name="result"></param>
        public bool SendContent(int x, int y, int fontSize, int width, int heigh, string content)
        {
            var flag = false;
            try
            {
                if (!User_RealtimeConnect(cardNumber))
                {
                    var asdfasd = cardNumber;
                    return flag;//连接实时通信失败
                }

                flag = SendText(x, y, fontSize, width, heigh, content);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DisposeConnect();//断开连接
            }
            return flag;
        }

        public void DrawRectangleFloat(PaintEventArgs e)
        {
            // Create pen.
            var blackPen = new Pen(Color.Black, 3);

            // Create location and size of rectangle.
            float x = 0.0F;
            float y = 0.0F;
            float width = 200.0F;
            float height = 200.0F;

            // Draw rectangle to screen.
            e.Graphics.DrawRectangle(blackPen, x, y, width, height);
        }

        public bool SendText(int x, int y, int fontSize, int width, int heigh, string content)
        {
            if (fontSize <= 0) fontSize = 12;
            var flag = false;
            var fontInfo = new User_FontSet
            {
                bFontBold = false,
                bFontItaic = false,
                bFontUnderline = false,
                colorFont = 0xFFFF,
                iFontSize = fontSize,
                strFontName = "宋体",
                iAlignStyle = 0,
                iVAlignerStyle = 0,
                iRowSpace = 0
            };

            if (!User_RealtimeSendText(cardNumber, x, y, width, heigh, content, ref fontInfo))
            {
                //MessageBox.Show("发送实时文本失败！");
            }
            else
            {
                //MessageBox.Show("发送实时文本成功！");
                flag = true;
            }
            return flag;
        }

        /// <summary>清屏</summary>
        public void ScreenClear()
        {
            User_RealtimeScreenClear(cardNumber);
        }

        #region Private method

        /// <summary>更新 System32 下的配置文件</summary>
        private void UpdateConfig()
        {
            var sytem32path = Environment.GetFolderPath(Environment.SpecialFolder.System);
            var fileName = "/EQ2008_Dll_Set.ini";

            var binConfig = "." + fileName;
            if (!File.Exists(binConfig)) return;

            var binFile = File.GetLastWriteTime(binConfig);
            var sysFile = File.GetLastWriteTime(sytem32path + fileName);

            if (binFile != sysFile) File.Copy(binConfig, sytem32path + fileName, true);
        }

        #endregion
    }
}
